package com.jianyun.jyzs.qrcode.barcodescanner;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.receive.NetworkConnectChangedReceiver;


/**
 *
 */
public class CaptureActivity extends Activity implements NetworkConnectChangedReceiver.NetworkChangeStatusListener {
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private NetworkConnectChangedReceiver mNetworkConnectChangedReceiver;

    private static int cameraPermissionReqCode = 250;
    private boolean askedPermission = false;

    /**
     * 需要进行检测的权限数组
     */
    protected String[] needPermissions = {
            Manifest.permission.CAMERA
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        barcodeScannerView = initializeContent();

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();


        IntentFilter filter = new IntentFilter();
        filter.addAction(NetworkConnectChangedReceiver.CONNECTIVITY_CHANGE);
        mNetworkConnectChangedReceiver = new NetworkConnectChangedReceiver();
        mNetworkConnectChangedReceiver.setNetworkChangeStatusListener(this);
        registerReceiver(mNetworkConnectChangedReceiver, filter);
    }



    /**
     * Override to use a different layout.
     *
     * @return the DecoratedBarcodeView
     */
    protected DecoratedBarcodeView initializeContent() {
        setContentView(R.layout.zxing_capture);
        return (DecoratedBarcodeView) findViewById(R.id.zxing_barcode_scanner);
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mNetworkConnectChangedReceiver);
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    public void finishPager(View view) {
        finish();
    }

    @Override
    public void onChange(boolean isHasNetwork) {
        barcodeScannerView.getViewFinder().networkChange(!isHasNetwork);
    }
}
